package com.atguigu.srb.core.controller.admin;


import com.atguigu.common.exception.Assert;
import com.atguigu.common.exception.BusinessException;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2022-11-22
 */
@RestController
@Api(tags = "积分等级管理模块")
@RequestMapping("/admin/integralGrade")
public class AdminIntegralGradeController {
    @Autowired
    private IntegralGradeService integralGradeService;
    @GetMapping
    @ApiOperation(value = "查询所有积分等级（积分等级列表）")
    public R getIntegralGradeList(){
        List<IntegralGrade> integralGradeList = integralGradeService.list();
        return R.ok().data("integralGradeList",integralGradeList).data("k","kk");
    }
    @GetMapping("{id}")
    @ApiOperation(value = "根据id查询积分等级列表")
    public R getIntegralGrade(@ApiParam(value = "id值",required = true) @PathVariable String id){
        IntegralGrade integralGrade = integralGradeService.getById(id);
        if (integralGrade!=null){
            return R.ok().data("integralGrade",integralGrade);
        }else {
            return R.error().message("数据不存在");
        }

    }
    @DeleteMapping("{id}")
    @ApiOperation(value = "根据id删除积分等级列表")
    public R deleteIntegralGrade(@PathVariable String id){
        boolean b = integralGradeService.removeById(id);
        if (b){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }
    @PutMapping
    @ApiOperation(value = "更新积分等级列表",notes = "必须有id")
    public R updateIntegralGrade(@RequestBody IntegralGrade integralGrade){
        boolean b = integralGradeService.updateById(integralGrade);
        if (b){
            return R.ok().message("更新成功");
        }else {
            return R.error().message("更新失败");
        }
    }
    @PostMapping
    @ApiOperation(value = "新增积分等级列表")
    public R saveIntegralGrade(@RequestBody IntegralGrade integralGrade){
//        if (integralGrade.getBorrowAmount()==null){
//            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
//        }
        Assert.notNull(integralGrade.getBorrowAmount(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        boolean b = integralGradeService.save(integralGrade);
        if (b){
            return R.ok().message("新增成功");
        }else {
            return R.error().message("新增失败");
        }
    }

}

